package com.bite.lesson2;

import java.util.Random;

public class Demo_01 {
    public static void main(String[] args) throws InterruptedException {
        //创建数组，生成随机数
        int[] array = new int[1000_0000];
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            int num = random.nextInt(100) + 1;
            array[i] = num;
        }

        Sum sum = new Sum();

        //初始时间
        long time01 = System.currentTimeMillis();

        //创建两个线程，分别计算，奇数偶数和
        Thread thread01 = new Thread(() -> {
            for (int i = 0; i < array.length; i += 2) {
                sum.sum01(array[i]);
            }
        });

        Thread thread02 = new Thread(() -> {
            for (int i = 1; i < array.length ; i += 2) {
                sum.sum02(array[i]);
            }
        });

        //启动线程
        thread01.start();
        thread02.start();

        //等待线程
        thread01.join();
        thread02.join();

        //结束时间
        long time02 = System.currentTimeMillis();

        System.out.println("结果为 ：" + sum.result());
        System.out.println("运行时间为 ：" + (time02 - time01));



    }
}


class Sum {

    long sum1 = 0;
    long sum2 = 0;
    public void sum01(int n) {
        sum1 += n;
    }

    public void sum02(int n) {
        sum2 += n;
    }

    public long result() {
        return sum1 + sum2;
    }
}
